﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;

namespace Telecom.Service
{
    public class rolesMenuSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();

        public decimal addorUpdate(ROLE_MENUS entity)
        {
            if (entity.RMID == 0) dbContext.ROLE_MENUS.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.RMID;
        }

        public List<ROLE_MENUS> getList()
        {
            try
            {
                return dbContext.ROLE_MENUS.ToList();
            }
            catch
            {
                return null;
            }
        }

        public List<ROLE_MENUS> getRoleMenuInRoles(int roles)
        {
            try
            {
                dbContext.Configuration.LazyLoadingEnabled = false;
                return dbContext.ROLE_MENUS.Where(p => p.ROLEID == roles).ToList();
            }
            catch
            {
                return null;
            }
        }

        public bool checkMenuExtisInRoles(decimal menuid, decimal roles)
        {
            try
            {
                var check = dbContext.ROLE_MENUS.Where(p => p.ROLEID == roles && p.MENUID == menuid).ToList();
                if (check != null && check.Count() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }

        public ROLE_MENUS GetById(int id)
        {
            try
            {
                return dbContext.ROLE_MENUS.Where(p => p.RMID == id).First();
            }
            catch
            {
                return null;
            }
        }

        public bool detele(int id)
        {
            try
            {
                var _entity = GetById(id);
                dbContext.ROLE_MENUS.Remove(_entity);
                dbContext.SaveChanges();
                return true;
            }
            catch { return false; }
        }

        public List<MENUS> ListMenuOnRoles(int RolesId)
        {
            try
            {
                var c = (from i in dbContext.MENUS
                         join k in dbContext.ROLE_MENUS on i.MENUID equals k.MENUID
                         join l in dbContext.ROLES on k.ROLEID equals l.ROLEID
                         where l.ROLEID == RolesId
                         select i).ToList();
                return c;
            }
            catch { return null; }
        }

        public List<MENUS> ListMenuOnRolesArray(decimal[] RolesId)
        {
            try
            {
                var c = (from i in dbContext.MENUS
                         join k in dbContext.ROLE_MENUS on i.MENUID equals k.MENUID
                         join l in dbContext.ROLES on k.ROLEID equals l.ROLEID
                         where RolesId.Contains(l.ROLEID)
                         select i).ToList();
                return c;
            }
            catch { return null; }
        }

        public List<MENUS> ListMenuOnRolesArrayAndMainMenu(decimal[] RolesId, int MainMenu)
        {
            try
            {
                // list Id Menu
                var listIdMenu = dbContext.ROLE_MENUS.Where(p => RolesId.Contains(p.ROLEID.Value)).GroupBy(p => p.MENUID).Select(p => p.Key).ToArray();

                // ds menu
                var listmenu = dbContext.MENUS.Where(p => listIdMenu.Contains(p.MENUID) && p.MAINMENU.Value == MainMenu).OrderBy(l => l.STT).ToList();

                return listmenu;

            }
            catch { return null; }
        }
    }
}
